package com.baymax.test.scene.demo

import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx2.awaitFirst
import java.io.File

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       PermissionScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/9/23 17:48
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class PermissionScene : BaseScene() {

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {
            addButton("use permission") {
                launch {
                    val r = RxPermissions(requireActivity())
                        .request(
                            WRITE_EXTERNAL_STORAGE,
                            READ_EXTERNAL_STORAGE
                        )
                        .awaitFirst()
                    Log.d(TAG, "onCreateView: MANAGE_EXTERNAL_STORAGE $r")
                }
            }
            addButton("use permission do some") {
                launch {

                    File("/sdcard/a.text").apply {
                        createNewFile()
                        appendText("hello")
                    }
                }
            }


        }
    }
}